﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;

public partial class Manage_EditTrain : System.Web.UI.Page
{
    protected void Page_Load(object sender, EventArgs e)
    {
        if (!IsPostBack)
        {
            #region 绑定年、月、日、时、分
            Year.Items.Clear();
            for (int i = 2012; i <= DateTime.Now.Year + 2 || i <= 2007 + 10; i++)
            {
                Year.Items.Add(new ListItem(Convert.ToString(i), Convert.ToString(i)));
            }
            Year.SelectedValue = Convert.ToString(DateTime.Now.Year);

            Month.Items.Clear();
            for (int i = 1; i <= 12; i++)
            {
                Month.Items.Add(new ListItem(Convert.ToString(i), Convert.ToString(i)));
            }
            Month.SelectedValue = Convert.ToString(DateTime.Now.Month);

            Day.Items.Clear();
            for (int i = 1; i <= 31; i++)
            {
                Day.Items.Add(new ListItem(Convert.ToString(i), Convert.ToString(i)));
            }
            Day.SelectedValue = Convert.ToString(DateTime.Now.Date);

            Hour.Items.Clear();
            for (int i = 0; i <= 23; i++)
            {
                Hour.Items.Add(new ListItem(Convert.ToString(i), Convert.ToString(i)));
            }
            Hour.SelectedValue = Convert.ToString(DateTime.Now.Hour);

            Minute.Items.Clear();
            for (int i = 0; i <= 59; i++)
            {
                Minute.Items.Add(new ListItem(Convert.ToString(i), Convert.ToString(i)));
            }
            Minute.SelectedValue = Convert.ToString(DateTime.Now.Minute);
            #endregion

            string iid = Request.Params["iid"];
            string tid = Request.Params["tid"];

            CmsDataContext db = new CmsDataContext();    
            if (iid != null)
            {
                InviteID.Value = iid;

                #region 从邀请登记项目
                Invite inv = (from aI in db.Invite
                              where aI.ID == iid
                              select aI).First();

                BelongLabel.Text = inv.Manager.Belong;
                Belong.Value = inv.Manager.Belong;
                Name.Text = inv.Title;
                KindLabel.Text = inv.Kind;
                SubjectLabel.Text = inv.Subject;
                CourseLabel.Text = inv.Course;

                Kind.Value = inv.Kind;
                Subject.Value = inv.Subject;
                Course.Value = inv.Course;

                Memo.Text = inv.Memo;

                if (inv.Accepted != null)
                {
                    AcceptedTeacher.Text = inv.Teacher.Name;
                    TeacherID.Value = inv.TeacherID;
                }
                #endregion
            }
            else if (tid != null)
            {
                TrainID.Value = tid;
                Train train;

                #region 显示项目信息
                train = (from aT in db.Train
                         where aT.ID == tid
                         select aT).First();

                BelongLabel.Text = train.Belong;
                Belong.Value = train.Belong;
                Name.Text = train.Name;
                Address.Text = train.Address;
                KindLabel.Text = train.Kind;
                SubjectLabel.Text = train.Subject;
                CourseLabel.Text = train.Course;

                Kind.Value = train.Kind;
                Subject.Value = train.Subject;
                Course.Value = train.Course;

                Memo.Text = train.Memo;
                Required.Text = train.Required;
                Amount.Text = Convert.ToString(train.Amount);

                AcceptedTeacher.Text = train.Teacher.Name;
                TeacherID.Value = train.TeacherID;

                Year.SelectedValue = Convert.ToString(train.Started.Year);
                Month.SelectedValue = Convert.ToString(train.Started.Month);
                Day.SelectedValue = Convert.ToString(train.Started.Day);
                Hour.SelectedValue = Convert.ToString(train.Started.Hour);
                Minute.SelectedValue = Convert.ToString(train.Started.Minute);
                #endregion

                #region 如果已经添加了课件，则默认为该培训已经结束，不允许再进行修改
                if (train.ArticleID != null)
                {
                    Save.Visible = false;
                }
                #endregion
            }
        }

    }
    protected void Save_Click(object sender, EventArgs e)
    {
        CmsDataContext db = new CmsDataContext();
        string tid = TrainID.Value.Trim();
        Train train;

        try
        {
            if (tid == "")
            {
                train = new Train();
                train.ID = Toolkit.NextUUID;
                train.RegisterID = Convert.ToString(Session["ManagerID"]);
                train.District = Convert.ToString(Session["District"]);
                train.Belong = Convert.ToString(Belong.Value.Trim());
                train.Registed = DateTime.Now;
                train.InviteID = InviteID.Value.Trim();

                db.Train.InsertOnSubmit(train);
            }
            else
            {
                train = (from aT in db.Train
                         where aT.ID == tid
                         select aT).First();
            }

            train.Name = Name.Text.Trim();

            DateTime dt = DateTime.Now;
            train.Started = DateTime.Parse(string.Format("{0}-{1}-{2} {3}:{4}", Year.SelectedValue, Month.SelectedValue, Day.SelectedValue, Hour.SelectedValue, Minute.SelectedValue));
            train.Ended = train.Started;
            train.Address = Address.Text.Trim();
            train.Kind = Kind.Value.Trim();
            train.Subject = Subject.Value.Trim();
            train.Course = Course.Value.Trim();
            train.Memo = Memo.Text.Trim();
            train.TeacherID = TeacherID.Value;
            train.Required = Required.Text.Trim();
            train.Amount = Convert.ToInt32(Amount.Text.Trim());

            db.SubmitChanges();

            MessageBox.Message = "培训项目信息登记成功。";
            MessageBox.Location = "./EditTrain.aspx?tid=" + train.ID;
        }
        catch (Exception ee)
        {
            MessageBox.Message = ee.Message;
        }
    }
}